package com.fmi.chesstern.strategy;

import java.util.ArrayList;

import com.fmi.chesstern.gameboard.GameBoard;
import com.fmi.chesstern.gameboard.GameField;
import com.fmi.chesstern.pieces.ChessPiece;
import com.fmi.chesstern.pieces.ChessPiece.PieceColor;

public class BishopStrategy implements IGameStrategy {

	public ArrayList<GameField> getValidMoves(GameField currentPosition, GameBoard board) {
		ArrayList<GameField> allMoves = new ArrayList<GameField>();
		
		PieceColor color = board.getPieces().get(currentPosition.toString()).getColor();
		int i = currentPosition.getRow();
		int j = currentPosition.getColumn();
		
		while ( i < 7 && j < 7) {
			i++;
			j++;
			GameField field = new GameField(i, j);
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			} else {
				allMoves.add(field);
			}
		}
		
		i = currentPosition.getRow();
		j = currentPosition.getColumn();
		
		while ( i < 7 && j > 0) {
			i++;
			j--;
			GameField field = new GameField(i, j);
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			} else {
				allMoves.add(field);
			}
		}
		
		i = currentPosition.getRow();
		j = currentPosition.getColumn();
		
		while ( i > 0 && j > 0) {
			i--;
			j--;
			GameField field = new GameField(i, j);
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			} else {
				allMoves.add(field);
			}
		}
		
		i = currentPosition.getRow();
		j = currentPosition.getColumn();
		
		while ( i > 0 && j < 7) {
			i--;
			j++;
			GameField field = new GameField(i, j);
			ChessPiece piece = board.getPieces().get(field.toString());
			if (piece != null) {
				if (piece.getColor() != color) {
					allMoves.add(field);
					break;
				} else {
					break;
				}
			} else {
				allMoves.add(field);
			}
		}
		
		ArrayList<GameField> moves = new ArrayList<GameField>();
		for (GameField move : allMoves) {
			if (GameField.isFieldCorrect(move)) {
				moves.add(move);
			}
		}
		return moves;
	}

}
